package org.misalen.web.support.mysqlcore;

import java.util.ResourceBundle;

import org.misalen.web.support.mysqlcore.ConnectionPool.PooledConnection;

/**
 * mysql 操作
 * 
 * @author guochao
 *
 */
public class DBManager {
	private static final String mysqlUrl;
	private static final String driverClassName;
	private static final String mysqlPassword;
	private static final String mysqlUserName;

	private static PooledConnection conn;
	private static ConnectionPool connectionPool;
	private static DBManager inst;

	static {
		ResourceBundle resb1 = ResourceBundle.getBundle("jdbc");
		driverClassName = resb1.getString("jdbc.driverClassName");
		mysqlUrl = resb1.getString("jdbc.url");
		mysqlPassword = resb1.getString("jdbc.password");
		mysqlUserName = resb1.getString("jdbc.username");

	}

	public void close() {
		connectionPool.closeConnectionPool();
	}

	public DBManager() {
		if (inst != null)
			return;
		connectionPool = new ConnectionPool(driverClassName, mysqlUrl, mysqlUserName, mysqlPassword);
		try {
			connectionPool.createPool();
			inst = this;
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public static PooledConnection getConnection() {
		if (inst == null)
			new DBManager();
		conn = connectionPool.getConnection();
		return conn;
	}
}
